home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_004 / bm / getpatfile.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  71 lines

  1. #include <stdio.h>
  2. #ifdef unix
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #endif
  6. #include "bm.h"
  7.  
  8. int
  9. GetPatFile(PatFile, DescVec)
  10. char *PatFile;
  11. struct PattDesc *DescVec[];
  12. /* read patterns from a file and set up a pattern descriptor vector */
  13. {
  14.     FILE *PFile;
  15. #ifdef unix
  16.     extern char *malloc();
  17.     struct stat StatBuff;
  18.     int PatSize; /* the number of chars in all the patterns */
  19.     char *PatBuff; /* hold the patterns */
  20.     if (!(PFile = fopen(PatFile,"r"))) {
  21.         fprintf(stderr,"bm: can't open pattern file %s\n",PatFile);
  22.         exit(2);
  23.     } /* if */
  24.     /* find out how big the patterns are */
  25.     if (fstat(fileno(PFile),&StatBuff) == -1) {
  26.         fprintf(stderr,"bm: can't fstat %s\n",PatFile);
  27.         exit(2);
  28.     } /* if */
  29.     PatSize = StatBuff.st_size;
  30.     if (!PatSize) {
  31.         fprintf(stderr,"bm: pattern file is empty\n");
  32.         exit(2);
  33.     } /* if */
  34.     if (!(PatBuff = malloc(PatSize))) {
  35.            fprintf(stderr,"bm: insufficient memory to store patterns\n");
  36.         exit(2);
  37.     } /* if */
  38.     fread(PatBuff,1,PatSize,PFile); /* get the patterns */
  39.     /* make sure the patterns are null-terminated. We can't have
  40.     * nulls in the patterns */
  41.     if (PatBuff[PatSize-1] == '\n')
  42.         PatBuff[PatSize-1] = '\0';
  43.     else
  44.         PatBuff[PatSize] = '\0';    /* BUG! Past allocated mem */
  45. #else
  46.     static char PatBuff[1024];
  47.     register char *PatBuffp = PatBuff;
  48.     register int inchar;
  49.     if (!(PFile = fopen(PatFile,"r"))) {
  50.         fprintf(stderr,"bm: can't open pattern file %s\n",PatFile);
  51.         exit(2);
  52.     } /* if */
  53.     while ((inchar = fgetc (PFile)) != EOF) {
  54.         if ((PatBuffp - PatBuff) >= sizeof (PatBuff)) {
  55.         fprintf(stderr,"bm: insufficient memory to store patterns\n");
  56.         exit(2);
  57.         }
  58.         *PatBuffp++ = inchar;
  59.     }
  60.     if (PatBuffp == PatBuff) {
  61.         fprintf(stderr,"bm: pattern file is empty\n");
  62.         exit(2);
  63.     } /* if */
  64.     /* make sure the patterns are null-terminated. We can't have
  65.     * nulls in the patterns */
  66.     if (*--PatBuffp == '\n')
  67.         *PatBuffp = '\0';
  68. #endif
  69.     return(MkDescVec(DescVec,PatBuff));
  70. } /* GetPatFile */
  71.